package com.uchedao.vehicledetection.utils;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.widget.Toast;

import com.uchedao.vehicledetection.application.UCheDaoApplication;

public class ToastUtil {
    private static Toast mToast;
    private static Handler handler;

    /**
     * 必须先在UI线程中调用
     */
    public static void init(Context context) {
        mToast = Toast.makeText(context, "", Toast.LENGTH_LONG);
        handler = new Handler() {

            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                mToast.setDuration(Toast.LENGTH_SHORT);
                mToast.setText(msg.obj.toString());
                mToast.show();
            }

        };
    }

    /**
     * 显示Toast 长时间
     *
     * @param messageId
     */
    public static void showToastLong(int messageId) {
        Context context = UCheDaoApplication.getContext();
        showToast(context.getString(messageId), false);
    }

    /**
     * 显示Toast 长时间
     *
     * @param message
     */
    public static void showToastLong(CharSequence message) {
        showToast(message, false);
    }

    /**
     * 显示Toast
        *
        * @param shortTime
                */
        public static void showToast(CharSequence message, boolean shortTime) {
            if (TextUtils.isEmpty(message))
                return;

            Message msg = new Message();
            msg.obj = message;
            msg.what = shortTime ? Toast.LENGTH_SHORT : Toast.LENGTH_LONG;
            handler.sendMessage(msg);

            // mToast.setText(message);
            // mToast.setDuration(shortTime ? Toast.LENGTH_SHORT :
        // Toast.LENGTH_LONG);
        // mToast.show();

    }

}
